//
//  NewsDetailVC.m
//  Buiom_7.0
//
//  Created by lanou3g on 15/10/30.
//  Copyright © 2015年 triffe. All rights reserved.
//

#import "NewsDetailVC.h"

@interface NewsDetailVC ()<UIWebViewDelegate,MONActivityIndicatorViewDelegate>

@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) NSString *urlString;
@property (nonatomic, strong) MONActivityIndicatorView *indicatorView;

@end

@implementation NewsDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // 创建webView
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    // 自适应屏幕
    self.webView.scalesPageToFit = YES;
    [self.view addSubview:_webView];
    self.webView.delegate = self;
    
    
    self.indicatorView = [[MONActivityIndicatorView alloc] init];
    _indicatorView.center = self.webView.center;
    CGPoint point = _indicatorView.center;
    point.y = self.webView.center.y - 200;
    _indicatorView.center = point;
    _indicatorView.delegate = self;
    [self.webView addSubview:_indicatorView];
    [_indicatorView startAnimating];
    [self setUpData];
    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"houtuiyibu"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:(UIBarButtonItemStylePlain) target:self action:@selector(actionLeft:)];
    self.navigationItem.leftBarButtonItem = left;
    
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"share"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:(UIBarButtonItemStylePlain) target:self action:@selector(actionright:)];
    self.navigationItem.rightBarButtonItem = right;
    
    // Do any additional setup after loading the view.
}

#pragma mark -- 添加加载圆圈动画的方法
- (UIColor *)activityIndicatorView:(MONActivityIndicatorView *)activityIndicatorView circleBackgroundColorAtIndex:(NSUInteger)index
{
    if (index == 0) {
        return [UIColor redColor];
    }if (index == 1) {
        return [UIColor magentaColor];
    }if (index == 2) {
        return [UIColor orangeColor];
    }if (index == 3) {
        return [UIColor purpleColor];
    }
    return [UIColor yellowColor];
}


// 实现方法
- (void)actionLeft:(UIBarButtonItem *)bar
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)actionright:(UIBarButtonItem *)bar
{
    NSString *shareString = [NSString stringWithFormat:@"%@",self.NewsTitle];
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"56374807e0f55a245800042a" shareText:shareString shareImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.imageURL]]] shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToTwitter,UMShareToDouban,UMShareToRenren,nil] delegate:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// 数据解析
- (void)setUpData
{
    NSString *string = [NSString stringWithFormat:@"http://api.m.mtime.cn/News/Detail.api?newsId=%@",self.newsID];
    NSURL *url = [NSURL URLWithString:string];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:30.0];
    [request setHTTPMethod:@"GET"];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        self.urlString = dictionary[@"url"];
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlString]]];
        [_indicatorView stopAnimating];
    }];
}








/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
